package com.yexin.qingzhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yexin.qingzhou.dto.LoginResponseDTO;
import com.yexin.qingzhou.dto.UserInfoDTO;
import com.yexin.qingzhou.dto.UserLoginDTO;
import com.yexin.qingzhou.dto.UserRegisterDTO;
import com.yexin.qingzhou.entity.User;
import com.yexin.qingzhou.exception.BusinessException;
import com.yexin.qingzhou.mapper.UserMapper;
import com.yexin.qingzhou.service.UserService;
import com.yexin.qingzhou.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    
    @Resource
    private JwtUtil jwtUtil;
    
    private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public LoginResponseDTO login(UserLoginDTO loginDTO) {
        User user = userMapper.selectOne(
            new QueryWrapper<User>().eq("username", loginDTO.getUsername())
        );
        
        if (user == null || !passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())) {
            throw new BusinessException("用户名或密码错误");
        }

        Map<String, Object> claims = new HashMap<>();
        claims.put("username", user.getUsername());
        claims.put("id", user.getId());
        String token = jwtUtil.genToken(claims);

        UserInfoDTO userInfo = new UserInfoDTO();
        userInfo.setId(user.getId());
        userInfo.setUsername(user.getUsername());
        userInfo.setEmail(user.getEmail());
        userInfo.setPhone(user.getPhone());
        userInfo.setCreateTime(user.getCreateTime());
        userInfo.setUpdateTime(user.getUpdateTime());

        return new LoginResponseDTO(token, userInfo);
    }

    @Override
    public User register(UserRegisterDTO registerDTO) {
        // 检查用户名是否已存在
        if (userMapper.selectOne(new QueryWrapper<User>().eq("username", registerDTO.getUsername())) != null) {
            throw new BusinessException("用户名已存在");
        }
        
        User user = new User();
        BeanUtils.copyProperties(registerDTO, user);
        user.setPassword(passwordEncoder.encode(registerDTO.getPassword()));
        
        userMapper.insert(user);
        return null;
    }

    @Override
    public User getUserById(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        return user;
    }

    @Override
    public Page<User> getUserList(int pageNum, int pageSize) {
        return userMapper.selectPage(new Page<>(pageNum, pageSize), null);
    }

    @Override
    public User updateUser(User user) {
        if (userMapper.updateById(user) > 0) {
            return userMapper.selectById(user.getId());
        }
        throw new BusinessException("更新用户信息失败");
    }

    @Override
    public void deleteUser(Long id) {
        if (userMapper.deleteById(id) <= 0) {
            throw new BusinessException("删除用户失败");
        }
    }
} 